using UnityEngine;
using System.Collections;


public class Attacker : Action {
	
	public int power = 10;
	public float speed = 0.33f;
	public float attackDecisiveTime = 0.15f;
	public float attackDuration = 0.1f;
	
	
	
	public GameObject HitEffectPos;
	protected bool isEnableHitEffect = false;
	
	protected bool isDecisive = false;
	protected float timer = 0;
	
	protected Defender[] attackedDefenders;
	
	[HideInInspector]
	public Weapon currentWeapon;
	public Weapon[] weapons;
	
	// Use this for initialization
	void Start () {
		currentWeapon = null;
		
		if(weapons == null || weapons.Length == 0)
		{
			print("Attacker: " + gameObject.name + " has no weapon.");
			return;
		}
		
		//make a new copy from pool
		currentWeapon = (Weapon)GameObject.Instantiate(weapons[0]);
		currentWeapon.transform.parent = transform;
		Transform cwTrans = currentWeapon.transform;
		cwTrans.localPosition = Vector3.zero;
		print("QAQ  " + currentWeapon);
		print("QwQ  " + currentWeapon.currentSkill);
		print("O3O  " + currentWeapon.currentSkill.getAnimationTime());
		speed = currentWeapon.currentSkill.getAnimationTime();
		attackDecisiveTime = currentWeapon.currentSkill.decisiveMoment;
		attackDuration = currentWeapon.currentSkill.hitDuration;
		
		//findWeaponFromPool(weaponName);
		
		if(currentWeapon != null)
		{
			print("Player: " + gameObject.name + " has Weapon: " + currentWeapon.gameObject.name);
		}else
		{
			print("Player: " + gameObject.name + " has no weapon");
		}
	}
	
	
	// find Weapon From Weapon Pool
	public void findWeaponFromPool(string weaponName)
	{
		GameObject weaponPool = GameObject.Find("WeaponPool");
		Weapon[] weaponPoolWeapons = weaponPool.transform.GetComponentsInChildren<Weapon>();
		
		foreach(Weapon weapon in weaponPoolWeapons)
		{
			if(weapon.gameObject.name == weaponName)
			{
				currentWeapon = (Weapon)GameObject.Instantiate(weapon);
				currentWeapon.transform.parent = transform;
				speed = currentWeapon.currentSkill.getAnimationTime();
				attackDecisiveTime = currentWeapon.currentSkill.decisiveMoment;
				attackDuration = currentWeapon.currentSkill.hitDuration;
				break;
			}
		}
	
	}
	
	// Update is called once per frame
	void Update () {
		if (state == null)
			state = GetComponent<ActorController>().actorState;
		
		if (state.actionState == ActionState.Attack)
		{
			timer += Time.deltaTime;
			if (timer > speed)
			{
				timer = 0;
				state.actionState = ActionState.Idle;
				state.animState = AnimationState.Idle;
				
				isDecisive = false;
				return;
			} else if ( (timer > attackDecisiveTime) && (!isDecisive) )
			          // (timer < attackDecisiveTime + attackDuration))
			{
				isDecisive = true;
				ProcessAttack();
			}
		}
	}
	
	public void startAttack() {
		state.actionState = ActionState.Attack;
	}
	
	public void finishAttack() {
		state.actionState = ActionState.Idle;
	}
	
	protected void ProcessAttack()
	{
		//onHitTarget(null);
		//parent of player must charactor game object which including all charactors(Player & AI)
		int size = transform.parent.childCount;
		
		Message.ALLAttacker(Message.DefaultMessage("Attack!!!"));
		
		Message.ALLDefender(Message.Attack(currentWeapon.currentSkill,this));
		
	}
	
	protected IEnumerator AllAttacker(Message msg)
	{
		if( msg.type == Message.MessageType.MSG)
		{
			print(msg.array[0]);
		} else if(msg.type == Message.MessageType.HIT_SUCCESS)
		{
			print(gameObject.name + " hit " + msg.sender.gameObject.name);
			if(!isEnableHitEffect && HitEffectPos != null && currentWeapon.HitEffect != null)
				this.StartCoroutine("onHitTarget",msg.sender.gameObject.name);
			
		}
		
		yield return null;
	}
	protected IEnumerator onHitTarget(string targetName)
	{
		
			//isEnableHitEffect = true;
			ParticleEmitter hitEffect =currentWeapon.HitEffect;
			
			//hitEffect.particleEmitter.Emit();
			hitEffect.emit = true;
			hitEffect.transform.parent = HitEffectPos.transform;
			hitEffect.transform.localPosition = Vector3.zero;
			yield return new WaitForSeconds( 0.1f);
			
			hitEffect.transform.parent = currentWeapon.transform;
			hitEffect.emit = false;
			
			//isEnableHitEffect = false;
		
	}
	public bool Attack()
	{
		if(state.actionState != ActionState.BeHit)
		{
			state.actionState = ActionState.Attack;
			state.animState = AnimationState.Action;
			string tempName = currentWeapon.currentSkill.GetRandomAnimation();
			UnityEngine.AnimationState tempState = animation[tempName];
			tempState.speed = 1.0f;
			animation.Play("Punch1");
			return true;
		}
		return false;
	}
}
